package com.ruoyi.resource.service.impl;

import java.util.Arrays;
import java.util.List;

import com.ruoyi.bussiness.domain.SysBusinessCheckList;
import com.ruoyi.bussiness.mapper.SysBusinessCheckListMapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.resource.mapper.SysBusinessResourceImageMapper;
import com.ruoyi.resource.domain.SysBusinessResourceImage;
import com.ruoyi.resource.service.ISysBusinessResourceImageService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 图片资源Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-22
 */
@Service
public class SysBusinessResourceImageServiceImpl implements ISysBusinessResourceImageService
{
    @Autowired
    private SysBusinessResourceImageMapper sysBusinessResourceImageMapper;

    @Autowired
    private SysBusinessCheckListMapper sysBusinessCheckListMapper;

    /**
     * 查询图片资源
     *
     * @param imageId 图片资源主键
     * @return 图片资源
     */
    @Override
    public SysBusinessResourceImage selectSysBusinessResourceImageByImageId(Long imageId)
    {
        return sysBusinessResourceImageMapper.selectSysBusinessResourceImageByImageId(imageId);
    }

    /**
     * 查询图片资源列表
     *
     * @param sysBusinessResourceImage 图片资源
     * @return 图片资源
     */
    @Override
    public List<SysBusinessResourceImage> selectSysBusinessResourceImageList(SysBusinessResourceImage sysBusinessResourceImage)
    {
        return sysBusinessResourceImageMapper.selectSysBusinessResourceImageList(sysBusinessResourceImage);
    }

    /**
     * 新增图片资源
     *
     * @param sysBusinessResourceImage 图片资源
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSysBusinessResourceImage(SysBusinessResourceImage sysBusinessResourceImage)
    {
        sysBusinessResourceImage.setCreateTime(DateUtils.getNowDate());
        if(StringUtils.isNull(sysBusinessResourceImage.getImageUrls())){
            return -2;
        }

        //桥梁编号-桥梁名称-名称-拍摄方向-作者
        SysBusinessCheckList sysBusinessCheckList = new SysBusinessCheckList();
        if(StringUtils.isNotNull(sysBusinessResourceImage.getCheckListId())){
            sysBusinessCheckList = sysBusinessCheckListMapper.selectSysBusinessCheckListByCheckListId(sysBusinessResourceImage.getCheckListId());
        }

        List<String> imageList =  Arrays.asList(sysBusinessResourceImage.getImageUrls().split(","));
        int row = 0;
        for(String s : imageList){
            SysBusinessResourceImage temp = new SysBusinessResourceImage();
            BeanUtils.copyProperties(sysBusinessResourceImage,temp);
            temp.setImageUrl(s);
            StringBuilder sb = new StringBuilder();
            if(sysBusinessCheckList != null){
                sb.append(getName(sysBusinessCheckList.getBridgeNum())+"-");
                sb.append(getName(sysBusinessCheckList.getBridgeName())+"-");
            }
            sb.append(getName(temp.getImageName())+"-");
            sb.append(getName(temp.getImageDirection())+"-");
            sb.append(getName(temp.getAuthor()));
            temp.setFileName(sb.toString());
            temp.setCreateBy(SecurityUtils.getUsername());
            row = sysBusinessResourceImageMapper.insertSysBusinessResourceImage(temp);
        }

        return 1;
    }

    //获取名称
    private String getName(Object o){
        String result = "";
        if(o == null){
            result = "00";
        }else {
            return o.toString();
        }

        return result;
    }

    /**
     * 修改图片资源
     *
     * @param sysBusinessResourceImage 图片资源
     * @return 结果
     */
    @Override
    public int updateSysBusinessResourceImage(SysBusinessResourceImage sysBusinessResourceImage)
    {
        sysBusinessResourceImage.setUpdateTime(DateUtils.getNowDate());
        sysBusinessResourceImage.setUpdateBy(SecurityUtils.getUsername());
        return sysBusinessResourceImageMapper.updateSysBusinessResourceImage(sysBusinessResourceImage);
    }

    /**
     * 批量删除图片资源
     *
     * @param imageIds 需要删除的图片资源主键
     * @return 结果
     */
    @Override
    public int deleteSysBusinessResourceImageByImageIds(Long[] imageIds)
    {
        return sysBusinessResourceImageMapper.deleteSysBusinessResourceImageByImageIds(imageIds);
    }

    /**
     * 删除图片资源信息
     *
     * @param imageId 图片资源主键
     * @return 结果
     */
    @Override
    public int deleteSysBusinessResourceImageByImageId(Long imageId)
    {
        return sysBusinessResourceImageMapper.deleteSysBusinessResourceImageByImageId(imageId);
    }
}
